//
//  MTNewPaymentHeaderView.swift
//  MT
//
//  Created by ooops on 2020/5/14.
//  Copyright © 2020 张强. All rights reserved.
//

import UIKit
import MTCategory

class MTNewPaymentHeaderView: UIView {
    class func reuseIdentity() -> String {
        return "MTNewPaymentHeaderViewId"
    }
    
    var money: Int? {
        didSet {
            if let m = money {
                amountLabel.attributedText = mtArrtibute(text: "\(m)", icon: "blue_dimond", position: 1)
            }
        }
    }
    
    lazy var tipLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCMediumFont(12)
        l.textColor = UIColor.text.x9b9b9b
        l.text = "我的钻石"
        return l
    }()

    lazy var amountLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCMediumFont(12)
        l.textColor = UIColor.text.x9b9b9b
        l.text = "10084"
        return l
    }()
    
    lazy var iconView: UIImageView = {
        let imageV = UIImageView(image: UIImage(named: "blue_dimond"))
        imageV.sizeToFit()
        return imageV
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        makeConstraint()
        backgroundColor = UIColor(hex: 0x2f2f2f)
        //amountLabel.attributedText = mtArrtibute(text: "10086", icon: "blue_dimond", position: 1)
    }
    
    private func mtArrtibute(text: String, icon: String, position: Int, iconSize: CGSize? = CGSize(width: 24, height: 24), iconYFix: CGFloat = -2, textColor: UIColor = UIColor.white) -> NSAttributedString {
        
        let markAttribute = NSMutableAttributedString(string: text)
        
        let arrtibuteValue: [NSAttributedString.Key : Any] = [NSAttributedString.Key.foregroundColor : textColor,
                                                             NSAttributedString.Key.font : PingFangSCSemiboldFont(20)]
        
        markAttribute.addAttributes(arrtibuteValue, range: NSRange(location: 0, length: text.count))
        
        let markattch = NSTextAttachment()
        
        markattch.image = UIImage(named: icon)
        
        markattch.bounds = CGRect(x: 0, y: iconYFix, width: iconSize!.width, height: iconSize!.height)
        
        let markattchStr = NSAttributedString(attachment: markattch)
        
        /// icon 插入的位置
        if position == 0 {
            markAttribute.insert(markattchStr, at: 0)
        } else {
            markAttribute.append(markattchStr)
        }
        
        return markAttribute
    }
    
    private func setupUI() {
        addSubview(tipLabel)
        addSubview(amountLabel)
        //addSubview(iconView)
    }
    
    private func makeConstraint() {
        tipLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(self.snp.centerY).offset(-1)
        }
        
        amountLabel.snp.makeConstraints { (make) in
            make.top.equalTo(self.snp.centerY).offset(1)
            //make.right.equalTo(tipLabel.snp.centerX).offset(-3)
            make.centerX.equalToSuperview()
        }
        
//        iconView.snp.makeConstraints { (make) in
//            make.centerY.equalTo(amountLabel)
//            make.left.equalTo(amountLabel.snp.right).offset(2)
//        }
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
}
